
# REQUIRES: Data/Roper Data.csv

################# Dependencies #################
# source("Load Packages.R")
# source("Analysis - Roper/Cleaning.R")

################# Figure 1b #################

### Scatter Plot
roper$startyear <-as.Date(roper$Startdate,'%Y-%m-%d')
roper$startyear <- format(as.Date(roper$startyear, format="%Y/%m/%d"),"%Y")
roper$startyear <- as.numeric(roper$startyear)

roper$endyear <-as.Date(roper$Enddate,'%Y-%m-%d')
roper$endyear <- format(as.Date(roper$endyear, format="%Y/%m/%d"),"%Y")
roper$endyear <- as.numeric(roper$endyear)

roper$surveyyear <- (roper$startyear+roper$endyear)/2

### Vietnam
roper_v <- roper[(roper$Campaign == "Vietnam"),]
roper_v <- roper_v %>% dplyr::select(Campaign, surveyyear, percwithdrawd)

plot(roper_v$surveyyear, roper_v$percwithdrawd, main="Public Support for Withdrawal Over Time - Vietnam War", xlab="Year", 
     ylab="Percent Withdrawal", ylim=c(0,1))
abline(lm(roper_v$percwithdrawd~roper_v$surveyyear), col="blue")
lines(roper_v$surveyyear, fitted(loess(roper_v$percwithdrawd ~ roper_v$surveyyear)), col="red")


### Iraq
roper_i <- roper[(roper$Campaign == "Iraq"),]
roper_i <- roper_i %>% dplyr::select(Campaign, surveyyear, percwithdrawd)

plot(roper_i$surveyyear, roper_i$percwithdrawd, main="Public Support for Withdrawal Over Time - Iraq War", xlab="Year", 
     ylab="Percent Withdrawal", ylim=c(0,1))
abline(lm(roper_i$percwithdrawd~roper_i$surveyyear), col="blue")
lines(roper_i$surveyyear, fitted(loess(roper_i$percwithdrawd ~ roper_i$surveyyear)), col="red")


### Afghanistan
roper_a <- roper[(roper$Campaign == "Afghanistan"),]
roper_a <- roper_a %>% dplyr::select(Campaign, surveyyear, percwithdrawd)

plot(roper_a$surveyyear, roper_a$percwithdrawd, main="Public Support for Withdrawal Over Time - Afghanistan War", xlab="Year", 
     ylab="Percent Withdrawal", ylim=c(0,1))
abline(lm(roper_a$percwithdrawd~roper_a$surveyyear), col="blue")
lines(roper_a$surveyyear, fitted(loess(roper_a$percwithdrawd ~ roper_a$surveyyear)), col="red")


### Figure 1
margin <- unit(0.5, "line")
grob1 <- ggplot(roper_v, aes(x=surveyyear, y=percwithdrawd)) +
  geom_point() +
  geom_smooth(method="loess", alpha=0.16, size=0.4, color="blue", se = FALSE) +
  geom_smooth(method="lm", size=0.4, color="red", se = FALSE) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits=c(0,1)) +
  scale_x_continuous(limits=c(1965,1972)) +
  labs(x = "Date of Poll", y = "Support for Withdrawal") +
  ggtitle("Vietnam") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5), text=element_text(family="Times New Roman", size = 18),
        panel.background = element_rect(fill = 'white', colour = 'white'),
        plot.background = element_rect(fill = 'white', colour = 'white'))

grob2 <- ggplot(roper_i, aes(x=surveyyear, y=percwithdrawd)) +
  geom_point() +
  geom_smooth(method="loess", alpha=0.16, size=0.4, color="blue", se = FALSE) +
  geom_smooth(method="lm", size=0.4, color="red", se = FALSE) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits=c(0,1)) +
  scale_x_continuous(limits=c(2003,2010)) +
  labs(x = "Date of Poll", y = "Support for Withdrawal") +
  ggtitle("Iraq") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5), text=element_text(family="Times New Roman", size = 18),
        panel.background = element_rect(fill = 'white', colour = 'white'),
        plot.background = element_rect(fill = 'white', colour = 'white'))

grob3 <- ggplot(roper_a, aes(x=surveyyear, y=percwithdrawd)) +
  geom_point() +
  geom_smooth(method="loess", alpha=0.16, size=0.4, color="blue", se = FALSE) +
  geom_smooth(method="lm", size=0.4, color="red", se = FALSE) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits=c(0,1)) +
  scale_x_continuous(limits=c(2000,2022)) +
  labs(x = "Date of Poll", y = "Support for Withdrawal") +
  ggtitle("Afghanistan") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5), text=element_text(family="Times New Roman", size = 18),
        panel.background = element_rect(fill = 'white', colour = 'white'),
        plot.background = element_rect(fill = 'white', colour = 'white'))

grob4 <- textGrob("Figure 2: U.S. Public Support for Withdrawal from Vietnam, Iraq, and Afghanistan Over Time", gp=gpar(fontsize=20,fontface = "bold", fontfamily="Times New Roman"))
plot1a <- grid.arrange(grob1, grob2, grob3, nrow = 1)

plot1 <- grid.arrange(plot1a, grob4,
                      heights = unit.c(unit(1,"null"), grobHeight(grob4) + 1.2*margin),
                      vp=viewport(width=0.95, height=0.95))

ggsave("Plots/Figure 2 - US Public Support for Withdrawal Over Time_VIA.png", plot1, width=16, height=5)

ggsave("Plots/Silverman POQ-23-0350.R1 Figure 2 Top.eps", grob1)
ggsave("Plots/Silverman POQ-23-0350.R1 Figure 2 Middle.eps", grob2)
ggsave("Plots/Silverman POQ-23-0350.R1 Figure 2 Bottom.eps", grob3)

rm(roper_v, roper_i, roper_a, grob1, grob2, grob3, grob4, plot1a, plot1, margin)
